package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysProductsDao;

import com.gusha.model.SysProducts;
import com.gusha.model.User;

@Service
@Transactional
public class SysProductsService {
@Autowired
private SysProductsDao sysProductsDao;
public Page<SysProducts> findAll(Pageable pageable, SysProducts sysProducts) {
    @SuppressWarnings("serial")
    Page<SysProducts> result = sysProductsDao.findAll(new Specification<SysProducts>() {
        @Override
        public Predicate toPredicate(Root<SysProducts> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = new ArrayList<Predicate>();
            if (sysProducts.getId() != null) {
                list.add(cb.equal(root.get("id").as(BigInteger.class),sysProducts.getId()));
            }
            if (sysProducts.getProductsName()!= null) {
                list.add(cb.equal(root.get("productsName").as(String.class),sysProducts.getProductsName()));
            }
            if (sysProducts.getAddTime()!= null) {
                list.add(cb.equal(root.get("addTime").as(Date.class),sysProducts.getAddTime()));
            }
            if (sysProducts.getUpdateTime()!= null) {
                list.add(cb.equal(root.get("updateTime").as(Date.class),sysProducts.getUpdateTime()));
            }
            if (sysProducts.getUser()!= null) {
                list.add(cb.equal(root.get("user").as(User.class),sysProducts.getUser()));
            }
            if (sysProducts.getPrice()!= null) {
                list.add(cb.equal(root.get("price").as(BigDecimal.class),sysProducts.getPrice()));
            }
            if (sysProducts.getProductsImg()!= null) {
                list.add(cb.equal(root.get("productsImg").as(String.class),sysProducts.getProductsImg()));
            }
            if (sysProducts.getProductsVideo()!= null) {
                list.add(cb.equal(root.get("productsVideo").as(String.class),sysProducts.getProductsVideo()));
            }
            if (sysProducts.getIsDelete()!= null) {
                list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysProducts.getIsDelete()));
            }
            if (sysProducts.getIsCheck()!= null) {
                list.add(cb.equal(root.get("isCheck").as(BigInteger.class),sysProducts.getIsCheck()));
            }
            if (sysProducts.getAddName()!= null) {
                list.add(cb.equal(root.get("addName").as(String.class),sysProducts.getAddName()));
            }
            if (sysProducts.getClickMun()!= null) {
                list.add(cb.equal(root.get("clickMun").as(Long.class),sysProducts.getClickMun()));
            }
            if (sysProducts.getProductsType()!= null) {
                list.add(cb.equal(root.get("productsType").as(BigInteger.class),sysProducts.getProductsType()));
            }
            if (sysProducts.getProContent()!= null) {
                list.add(cb.equal(root.get("proContent").as(String.class),sysProducts.getProContent()));
            }
            if (sysProducts.getStorageMode()!= null) {
                list.add(cb.equal(root.get("storageMode").as(BigInteger.class),sysProducts.getStorageMode()));
            }
            if (sysProducts.getProPackage()!= null) {
                list.add(cb.equal(root.get("proPackage").as(BigInteger.class),sysProducts.getProPackage()));
            }
            if (sysProducts.getProColour()!= null) {
                list.add(cb.equal(root.get("proColour").as(String.class),sysProducts.getProColour()));
            }
            if (sysProducts.getMerchantName()!= null) {
                list.add(cb.equal(root.get("merchantName").as(String.class),sysProducts.getMerchantName()));
            }
            if (sysProducts.getBusinessman()!= null) {
                list.add(cb.equal(root.get("businessman").as(String.class),sysProducts.getBusinessman()));
            }
            if (sysProducts.getStoreAddress()!= null) {
                list.add(cb.equal(root.get("storeAddress").as(String.class),sysProducts.getStoreAddress()));
            }
            if (sysProducts.getProContact()!= null) {
                list.add(cb.equal(root.get("proContact").as(String.class),sysProducts.getProContact()));
            }
            if (sysProducts.getProVolume()!= null) {
                list.add(cb.equal(root.get("proVolume").as(String.class),sysProducts.getProVolume()));
            }
         
            
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }
    },pageable);
     return result;
}
}
